import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-06-04
 * Time: 15:33
 */
class Solution {
    /*public int removeElement(int[] nums, int val) {
        int length = nums.length;
        int i = 0;
        for (;i < length ; i++) {
            if (nums[i] == val) {
                for (int j = i; j < length - 1; j++) {
                    nums[j] = nums[j + 1];
                }
                nums[length - 1] = val;
            }
            if(nums[i] == val) {
                i--;
            }
        }
        int count = 0;
        for (int k = 0; k < length; k++) {
            if(nums[k] != val) {
                count++;
            }
        }
        return count;
    }*/

    public int removeElement(int[] nums, int val) {
/*      int length = nums.length;
      int[] array = new int[length];
      int j = 0;
        int count = 0;
        for (int k = 0; k < length; k++) {
            if(nums[k] != val) {
                count++;
            }
        }
        for (int i = 0; i < length; i++) {
            if(j == count - 1) {
                break;
            }
            for (int k = j; k < length; k++) {
                if (nums[k] == val) {
                    j++;
                }else {
                    break;
                }
            }
            array[i] = nums[j];
            j++;
        }
        for (int i = count; i < length; i++) {
            array[i] = val;
        }
        for (int i = 0; i < length; i++) {
            nums[i] = array[i];
        }
        return count;*/

        int length = nums.length;
        int[] array = new int[length];
        int i = 0;
            for (int j = 0; j < length; j++) {
                if (nums[j] != val) {
                    array[i] = nums[j];
                    i++;
                }
            }
        for (int j = i; j < length; j++) {
            array[j] = val;
        }
        for (int j = 0; j < length; j++) {
            nums[j] = array[j];
        }
        return i;
    }
    public static void main(String[] args) {
        int[] nums = {3,2,2,3};
        Solution solution = new Solution();
        System.out.println(solution.removeElement(nums, 3));
        System.out.println(Arrays.toString(nums));
    }
}